<template>
<div>
<van-list
  class="x"
  v-model="loading"
  :finished="finished"
  finished-text="没有更多了"
  @load="onLoad"
   :error.sync="error"
  error-text="请求失败，点击重新加载"
>
  <van-cell v-for="(item, index) in list" :key="index" :title="item.aut_name" />
</van-list>
</div>
</template>

<script>
import { searchFn } from '@/api/search.js'
export default {
  props: {
    val: {
      type: String,
      required: true
    }
  },
  data () {
    return {
      list: [],
      loading: false,
      finished: false,
      page: 1,
      per_page: 20,
      error: false
    }
  },
  methods: {
    async onLoad () {
      // 异步更新数据
      // setTimeout 仅做示例，真实场景中一般为 ajax 请求
      try {
        const res = await searchFn({
          page: this.page,
          per_page: this.per_page,
          q: this.val
        })
        this.$toast('获取成功')
        console.log(res)
        this.list.push(...res.data.data.results)
        this.loading = false
        if (res.data.data.results.length) {
          this.page++
        } else {
          this.finished = true
        }
      } catch {
        this.$toast('获取失败')
        this.loading = false
        this.error = true
      }
    }
  }
}

</script>

<style lang="less" scoped>
</style>
